package com.example.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.application.BaseActivity;
import com.example.application.MyApplication;
import com.example.db.DBService;
import com.example.zhaccount.MainActivity;
import com.example.zhaccount.R;

public class UserNameActivity extends BaseActivity {
	private MyApplication myApp;
	private String regsitName;
	private EditText edUsername,edPass;
	private Button btnLogin;
	private  TextView tvRegist;
	
	private boolean loop = false;
	private List<String> mListName;
	private DBService mdbSevice;
	private Handler mHandler;
	private static final int MESS_NULL = 12;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_name);
		regsitName = getIntent().getStringExtra("name");
		myApp = (MyApplication) getApplication();
		findView();
		initHandler();
	}

	private void findView(){
		edUsername = (EditText) findViewById(R.id.username_text);
		edPass = (EditText) findViewById(R.id.password_text);
		btnLogin = (Button) findViewById(R.id.login_btn);
		tvRegist = (TextView) findViewById(R.id.register_text);
		
		edUsername.setText(regsitName);
		btnLogin.setOnClickListener(new MyOnClick());
		tvRegist.setOnClickListener(new MyOnClick());
		
		mdbSevice = new DBService(this);
		mListName = new ArrayList<String>();
	}
	
	private void initHandler(){
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == MESS_NULL) {
					Toast.makeText(UserNameActivity.this,
							"该用户名不存在", Toast.LENGTH_SHORT).show();
				}
			}
		};
	}
	
	private class MyOnClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.register_text:
				Intent it = new Intent(UserNameActivity.this, 
						RegisterUserActivity.class);
				startActivity(it);
				break;
			case R.id.login_btn:
				mListName = mdbSevice.findAllUser();
				String name = edUsername.getText().toString().trim();
				String pass = edPass.getText().toString().trim();
				if (name != null && !name.equals("") 
						&& pass != null && !pass.equals("")) {
					for (String str: mListName) {
						if (str.equals(name)) {
							String dbPass = mdbSevice.finaPass(name);
							if (dbPass.equals(pass)) {
								//把用户名设置为共享
								myApp.setUserName(name);
								myApp.setPass(dbPass);
								Intent intent = new Intent(
										UserNameActivity.this, MainActivity.class);
								startActivity(intent);
							}else{
								Toast.makeText(UserNameActivity.this,
										"密码输入不正确", Toast.LENGTH_SHORT).show();
							}
							loop = true;
							break;
						}
					}
					if (!loop) {
						Message mess = new Message();
						mess.what = MESS_NULL;
						mHandler.sendMessage(mess);
					}
					
				}else{
					Toast.makeText(UserNameActivity.this,
							"用户名密码不能为空", Toast.LENGTH_SHORT).show();
				}
				break;
			default:
				break;
			}
		}
		
	} 
}
